


ssc install binscatter

use "${pathdata_baseline}/BaselineExperiment.dta", clear

** ########## Delayed vs immediate Posteriors ##########

* Ensure the `statistic` variable is correctly set
gen statistic = 0
replace statistic = 1 if inlist(condition_, "statistic_neg", "statistic_pos")
replace diff_recall = diff_recall + 50
replace diff = diff + 50

* Create bins for group 0 (statistic == 0)
preserve
keep if statistic == 0
sort diff
egen bin_group0 = cut(diff), group(15)
bys bin_group0: egen mean_diff_recall0 = mean(diff_recall)
bys bin_group0: egen mean_diff0 = mean(diff)
gen group = 0
keep mean_diff_recall0 mean_diff0 group
tempfile bins0
save `bins0'
restore

* Create bins for group 1 (statistic == 1)
preserve
keep if statistic == 1
sort diff
egen bin_group1 = cut(diff), group(15)
bys bin_group1: egen mean_diff_recall1 = mean(diff_recall)
bys bin_group1: egen mean_diff1 = mean(diff)
gen group = 1
keep mean_diff_recall1 mean_diff1 group
tempfile bins1
save `bins1'
restore

* Load bins for group 0
use `bins0', clear
rename mean_diff_recall0 bin_y
rename mean_diff0 bin_x

* Save data for plotting
tempfile plot_data0
save `plot_data0'

* Load bins for group 1
use `bins1', clear
rename mean_diff_recall1 bin_y
rename mean_diff1 bin_x

* Save data for plotting
tempfile plot_data1
save `plot_data1'

* Combine the datasets for plotting
use `plot_data0', clear
append using `plot_data1'

* Plot the binned data with regression lines
twoway (scatter bin_y bin_x if group == 0, msymbol(d) mcolor(black*0.5) msize(large)) ///
       (scatter bin_y bin_x if group == 1, msymbol(o) mcolor(black*1.5) msize(large)) ///
       (lfit bin_y bin_x if group == 0, lcolor(black*0.5) lpattern(solid)) ///
       (lfit bin_y bin_x if group == 1, lcolor(black*1.5) lpattern(dash)), ///
    legend(order(1 "Story" 2 "Statistic" 3 "Regression Line Story" 4 "Regression Line Statistic")) ///
    xtitle("Posterior in {it:Immediate}") ///
    ytitle("Posterior in {it:Delay}") ///
    ylabel(20(10)80, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
    xlabel(0(10)100) ///
    xsc(r(-10 110) lcolor(none)) ysc(lcolor(none)) ///
    graphregion(color(white)) ///
    title("Delayed versus immediate posteriors", margin(b=3) color(black)) ///
    ysize(5) xsize(10)

* Export the graph
graph export "${pathout_baseline}/figures/figure2a.pdf", replace







use "${pathdata_baseline}/BaselineExperimentRecall.dta", clear
* Ensure the `statistic` variable is correctly set
* Ensure the `statisticcondition` variable is correctly set

replace diff_recall = diff_recall + 50
replace diff = diff + 50

** ########## Correct recall by immediate posterior ##########

* Create bins for group 0 (statisticcondition == 0)
preserve
keep if statisticcondition == 0
sort diff
egen bin_group0 = cut(diff), group(15)
bys bin_group0: egen mean_recallcombined0 = mean(recallcombined)
bys bin_group0: egen mean_diff0 = mean(diff)
gen group = 0
keep mean_recallcombined0 mean_diff0 group
tempfile bins0
save `bins0'
restore

* Create bins for group 1 (statisticcondition == 1)
preserve
keep if statisticcondition == 1
sort diff
egen bin_group1 = cut(diff), group(15)
bys bin_group1: egen mean_recallcombined1 = mean(recallcombined)
bys bin_group1: egen mean_diff1 = mean(diff)
gen group = 1
keep mean_recallcombined1 mean_diff1 group
tempfile bins1
save `bins1'
restore

* Load bins for group 0
use `bins0', clear
rename mean_recallcombined0 bin_y
rename mean_diff0 bin_x

* Save data for plotting
tempfile plot_data0
save `plot_data0'

* Load bins for group 1
use `bins1', clear
rename mean_recallcombined1 bin_y
rename mean_diff1 bin_x

* Save data for plotting
tempfile plot_data1
save `plot_data1'

* Combine the datasets for plotting
use `plot_data0', clear
append using `plot_data1'

* Plot the binned data with regression lines
twoway (scatter bin_y bin_x if group == 0, msymbol(d) mcolor(black*0.5) msize(large) ) ///
       (scatter bin_y bin_x if group == 1, msymbol(o) mcolor(black*1.5) msize(large) ) ///
       (lfit bin_y bin_x if group == 0, lcolor(black*0.5) lpattern(solid)) ///
       (lfit bin_y bin_x if group == 1, lcolor(black*1.5) lpattern(dash)), ///
    legend(order(1 "Story" 2 "Statistic" 3 "Regression Line Story" 4 "Regression Line Statistic")) ///
    xtitle("Posterior in {it:Immediate}") ///
    ytitle("Mean rate") ///
    ylabel(0(0.1)1, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
    yline(0, lcolor(gs5) lwidth(thin)) ///
    xlabel(0(10)100) ///
    xsc(r(-10 110) lcolor(none)) ysc(lcolor(none)) ///
    graphregion(color(white)) ///
    title("Correct recall by immediate posterior", color(black)) ///
    ysize(5) xsize(10)

* Export the graph
graph export "${pathout_baseline}/figures/figure2b.pdf", replace
